<?php 
class UsuarioDB
{
    public $var = 'a default value';

	public function getById($id){
		return mysql_query("SELECT * FROM `usuario` WHERE id = ".stripslashes($id));
	}
	
	public function getByEmail($email){
		return mysql_query("SELECT * FROM `usuario` WHERE email='".stripslashes($email)."'");
	}
	public function insert($data){
		$cpf = "";
	    if(isset($data["cpf"])){
	    	$signs = array("-", ".");
			$cpf = str_replace($signs, "", $data["cpf"]);
	    }
	    
	    $data_nasc_string = $data["dataNasc"];
	    $data_nasc = substr($data_nasc_string,6,4) . "-" . substr($data_nasc_string,3,2) . "-" . substr($data_nasc_string,0,2);
	    $inserted = mysql_query(
			"INSERT usuario (nome, email, senha, rg, rg_oe, rg_uf, cpf, endereco, cidade, estado, sexo, data_nasc, ativo, tipo, lattes, ddd_telefone, telefone, ddd_celular, celular, data_criacao) " .
			"VALUES (" .
				"'".stripslashes($data["nome"])."', ".
				"'".stripslashes($data["email"])."', ".
				"'".md5(stripslashes($data["pwd"]))."', " .
				"'".stripslashes($data["rg"])."', " .
				"'".stripslashes($data["rg_oe"])."', " .
				"'".stripslashes($data["rg_uf"])."', " .
				"'".stripslashes($cpf)."', " .
				"'".stripslashes($data["address"])."', " .
				"'".stripslashes($data["cidade"])."', " .
				"'".stripslashes($data["uf"])."', " .
				"'".stripslashes($data["sex"])."', " .
				"'".$data_nasc."', " .
				"1 , " .
				"'ALUNO', " .
				"'".stripslashes($data["lattes"])."', " .
				"'".stripslashes($data["phone-ddd"])."', " .
				"'".stripslashes($data["phone-nb"])."', " .
				"'".stripslashes($data["cell-ddd"])."', " .
				"'".stripslashes($data["cell-nb"])."', " .
				"CURDATE()" .
			");");
		
			return $inserted;	
	}    
    public function update($data){
		$cpf = "";
	    if(isset($data["cpf"])){
	    	$signs = array("-", ".");
			$cpf = str_replace($signs, "", $data["cpf"]);
	    }
	    
	    $data_nasc_string = $data["dataNasc"];
	    $data_nasc = substr($data_nasc_string,6,4) . "-" . substr($data_nasc_string,3,2) . "-" . substr($data_nasc_string,0,2);
	    $sql = 
			"UPDATE usuario SET " .
				" nome = '".stripslashes($data["nome"])."', ".
				" rg = '".stripslashes($data["rg"])."', " .
				" rg_oe = '".stripslashes($data["rg_oe"])."', " .
				" rg_uf = '".stripslashes($data["rg_uf"])."', " .
				" cpf = '".stripslashes($cpf)."', " .
				" endereco = '".stripslashes($data["address"])."', " .
				" cidade = '".stripslashes($data["cidade"])."', " .
				" estado = '".stripslashes($data["uf"])."', " .
				" sexo = '".stripslashes($data["sex"])."', " .
				" data_nasc = '".$data_nasc."', " .
				" lattes = '".stripslashes($data["lattes"])."', " .
				" ddd_telefone = '".stripslashes($data["phone-ddd"])."', " .
				" telefone = '".stripslashes($data["phone-nb"])."', " .
				" ddd_celular = '".stripslashes($data["cell-ddd"])."', " .
				" celular = '".stripslashes($data["cell-nb"])."', " .
				" data_edicao = CURDATE()" .
			" WHERE id = ".$data["id"];
			 $inserted = mysql_query($sql);
			
			return $inserted;	
	}    
	public function getProfessores(){
		return mysql_query("SELECT * FROM `usuario` WHERE tipo = 'PROFESSOR'");
	}
	public function getList($filters, $order){
		$sql = "SELECT * FROM usuario WHERE 1";
		if(isset($filters) && is_array($filters)){
			foreach ($filters as $field => $value) {
				if(isset($value)){
					$sql .= " and ".stripslashes($field." ".$value);
				}
			}
		}
		
		if(!isset($order) || $order != null){
			$order = "nome ASC";
		}
		$sql .= " ORDER BY ".$order;
//		die($sql);
		return mysql_query($sql);
	}
}



?>